package com.huicharge.huichargecrm.exception;

import com.huicharge.huichargecrm.result.Result;
import com.huicharge.huichargecrm.result.ResultCode;
import com.huicharge.huichargecrm.result.ResultCodes;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 鲁昊天
 * @date 2024/12/19
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /*
     *业务异常处理
     * */
    @ResponseBody
    @ExceptionHandler(BusinessException.class)
    public Result<?> businessExceptionHandler(BusinessException e) {
        log.error("业务异常捕获：{}", e.getMessage());
        return new Result(new ResultCode(e.getCode(), e.getMessage()));
    }

    /**
     * 全局未知异常
     */
    @ResponseBody
    @ExceptionHandler(Exception.class)
    public Result<String> exceptionHandler(Exception e) {
        log.error("全局未知异常捕获：{}", e.getMessage());
        return new Result<>(ResultCodes.SYSTEM_EXCEPTION);
    }

    public static void main(String[] args) {
        ListNode node = new ListNode(0);
        System.out.println(sortList(node));
    }

    /**
     * 分治(把链表拆分成多个唯一元素，唯一元素一定有序)
     */
    public static ListNode sortList(ListNode head) {
        //先检查链表是否唯一元素
        if (head == null) {
            return head;
        }
        if (head.next == null) {
            return head;
        }
        /*
         *把链表从中间拆分
         */
        //快慢指针
        ListNode fast = head, slow = head;
        //慢指针前面的节点
        ListNode preslow = null;
        while (fast.next != null) {
            preslow = slow;
            fast = fast.next.next;
            slow = slow.next;
            if (fast == null) {
                break;
            }
        }
        //第二个链表
        ListNode head2 = slow;
        //第一个链表
        preslow.next = null;
        ListNode head1 = head;
        /*
         * 拆分后继续拆分，直至被全部拆分成单个节点
         *
         * */
        ListNode leftres = sortList(head1);
        ListNode rightres = sortList(head2);
        return merge(leftres, rightres);
    }

    /**
     * merge
     */
    public static ListNode merge(ListNode l1, ListNode l2) {
        ListNode res = new ListNode();
        ListNode cur=res;//指针
        while (l1 != null || l2 != null) {
            if (l1 == null) {
                cur.next = l2;
                break;
            }
            if (l2 == null) {
                cur.next = l1;
                break;
            }
            if (l1.val <= l2.val) {
                cur.next=l1;
                l1=l1.next;
            } else {
                cur.next=l2;
                l2=l2.next;
            }
        }
        return res.next;
    }

    public static class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}

